﻿Imports System.Windows.Forms.OpenFileDialog
Imports System.Object
Imports System.MarshalByRefObject
Imports System.ComponentModel.Component
Imports System.Windows.Forms.CommonDialog
Imports System.Windows.Forms
Imports System.Data.OleDb
Imports System.IO
Imports System.Drawing
Imports System.Data.Odbc
Imports System.Data.Odbc.OdbcConnection
Imports System.Data.Odbc.OdbcCommand



Public Class frmEmpleado
    Public photo() As Byte
    Dim IMG As Drawing.Image
    Dim Pic As Byte()
    Dim globalimg As String
    Dim MyStream As New IO.MemoryStream
    Dim x As Integer = 0
    Dim modifica As Integer = 0
    Public filename As String



    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False

            GroupControl1.Enabled = True
            PanelAbajo.Enabled = False

        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            modificar.Enabled = True
            eliminar.Enabled = True
            imprimir.Enabled = True

            GroupControl1.Enabled = False
            PanelAbajo.Enabled = True

        End If
    End Sub
  

    Private Sub agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles agregar.Click
        IdhorarioLookUpEdit.EditValue = Nothing

        Fotografia1PictureBox.Image = My.Resources.url2
        EstaactivoCheckBox.CheckState = CheckState.Checked
        IdempleadoTextBox.EditValue = Nothing
        IdcargoLookUpEdit.EditValue = Nothing
        NombreTextBox.Text = ""
        ApellidoTextBox.Text = ""
        DireccionTextBox.Text = ""
        SexoComboBox.DisplayMember = ""
        CheckBox1.CheckState = CheckState.Unchecked
        EstadocivilComboBox.Text = ""
        IdcargoLookUpEdit.EditValue = Nothing

        Telefono1TextBox.Text = ""


        CorreoelectronicoTextBox.Text = ""
        Telefono2TextBox.Text = ""
        DireccionTextBox.Text = ""
        ObservacionTextBox.Text = ""
        PesoTextEdit1.Text = ""
        EstaturaTextEdit1.Text = ""


        IdhorarioLookUpEdit.EditValue = ""
        SueldobaseTextEdit.EditValue = ""
        ObservacionTextBox.Text = ""
        'BindingNavigatorAddNewItem.PerformClick()
        '  BindingNavigatorAddNewItem.PerformClick()
        modifica = 0
        Mostrar()

    End Sub

    Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles modificar.Click
        modifica = 1

        Mostrar()

    End Sub

    Private Sub eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminar.Click
        Dim opcion As Integer
        opcion = MsgBox("¿Está seguro que desea eliminar el registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion = vbYes Then
            Try
                EmpleadosTableAdapter.DeleteQuery(IdempleadoTextBox.Text)
                Me.EmpleadosTableAdapter.Fill(Me.DbFireAndIceDataSet.empleados)
            Catch ex As Exception
                MsgBox("Lo sentimos, el registro no puede ser eliminado ya que está relacionado con otras tablas", MsgBoxStyle.Critical, "Error")
            End Try


        End If
    End Sub

    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click
        FechaingresoDateTimePicker.Value = FechaingresoDateTimePicker.Value.Date.ToString("dd/MM/yyyy")
        FechaegresoDateTimePicker.Value = FechaingresoDateTimePicker.Value.Date.ToString("dd/MM/yyyy")
        FechanacimientoDateTimePicker.Value = FechaingresoDateTimePicker.Value.Date.ToString("dd/MM/yyyy")


       
        If IdempleadoTextBox.Text = "" Then

            ErrorProvider1.SetError(IdempleadoTextBox, "Ingrese una identificación")
            Return
        End If
        ErrorProvider1.SetError(IdempleadoTextBox, Nothing)

        Try
            Dim xx As Integer

            xx = EmpleadosTableAdapter.ScalarQuerySaberSiExisteEmpleado(TextBox22.Text)


            If xx = 1 Then

                ErrorProvider1.SetError(IdempleadoTextBox, "Ya existe un registro con esta identidad")
                Return
            End If
            ErrorProvider1.SetError(IdempleadoTextBox, Nothing)

        Catch ex As Exception

        End Try
       

        If (NombreTextBox.Text = "") Then
            ErrorProvider1.SetError(NombreTextBox, "Debe ingresar un nombre")
            Return
        End If

        ErrorProvider1.SetError(NombreTextBox, Nothing)


        If (ApellidoTextBox.Text = "") Then
            ErrorProvider1.SetError(ApellidoTextBox, "Debe ingresar un apellido")
            Return
        End If

        ErrorProvider1.SetError(ApellidoTextBox, Nothing)


        If (SexoComboBox.Text = "") Then
            ErrorProvider1.SetError(SexoComboBox, "Debe selecionar una opción")
            Return
        End If
        ErrorProvider1.SetError(SexoComboBox, Nothing)




        If (EstadocivilComboBox.Text = "") Then
            ErrorProvider1.SetError(EstadocivilComboBox, "Debe serleccionar una opción")
            Return
        End If
        ErrorProvider1.SetError(EstadocivilComboBox, Nothing)


        If EstaactivoCheckBox.CheckState = CheckState.Indeterminate Then
            ErrorProvider1.SetError(EstaactivoCheckBox, "Verifique su estado")
            Return
        End If
        ErrorProvider1.SetError(EstaactivoCheckBox, Nothing)


        If (IdcargoLookUpEdit.Text = "") Then

            ErrorProvider1.SetError(IdcargoLookUpEdit, "Debe ingresar un cargo")
            Return
        End If
        ErrorProvider1.SetError(IdcargoLookUpEdit, Nothing)



        If (IdhorarioLookUpEdit.Text = "") Then

            ErrorProvider1.SetError(IdhorarioLookUpEdit, "Debe seleccionar un horario para el paciente")
            Return
        End If
        ErrorProvider1.SetError(IdhorarioLookUpEdit, Nothing)



        Try
            If modifica = 1 Then

                EmpleadosTableAdapter.UpdateQuerytodo(NombreTextBox.Text, ApellidoTextBox.Text, FechanacimientoDateTimePicker.Value, SexoComboBox.Text, SueldobaseTextEdit.Text, FechaingresoDateTimePicker.Value, "", Telefono1TextBox.Text, Telefono2TextBox.Text, DireccionTextBox.Text, EstadocivilComboBox.Text, PesoTextEdit1.Text, EstaturaTextEdit1.Text, CorreoelectronicoTextBox.Text, ObservacionTextBox.Text, FechaegresoDateTimePicker.Value, IdcargoLookUpEdit.EditValue, photo, IdhorarioLookUpEdit.EditValue, CheckBox1.CheckState, IdempleadoTextBox.EditValue, CheckBox1.CheckState, IdempleadoTextBox.EditValue)
                Me.EmpleadosTableAdapter.Fill(Me.DbFireAndIceDataSet.empleados)
            Else


                'EmpleadosBindingNavigatorSaveItem.PerformClick()
                EmpleadosTableAdapter.InsertQuery(NombreTextBox.Text, ApellidoTextBox.Text, FechanacimientoDateTimePicker.Value, SexoComboBox.Text, SueldobaseTextEdit.Text, FechaingresoDateTimePicker.Value, "", Telefono1TextBox.Text, Telefono2TextBox.Text, DireccionTextBox.Text, EstadocivilComboBox.Text, PesoTextEdit1.Text, EstaturaTextEdit1.Text, CorreoelectronicoTextBox.Text, True, ObservacionTextBox.Text, FechaegresoDateTimePicker.Value, IdcargoLookUpEdit.EditValue, photo, IdhorarioLookUpEdit.EditValue, CheckBox1.CheckState, IdempleadoTextBox.EditValue)
                Me.EmpleadosTableAdapter.Fill(Me.DbFireAndIceDataSet.empleados)

            End If

        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "Nose pudo guardar el registro")
            Mostrar()
        End Try

        AlertControl1.AppearanceCaption.ForeColor = Color.Green
        AlertControl1.Show(Me, "Advertencia", My.Resources.StringGlobal.msjGuardarExito)
        Mostrar()







    End Sub


    Private Sub imprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imprimir.Click
        GridControl1.ShowPrintPreview()

    End Sub

  


    Public Shared Function getphoto(ByVal filePath As String) As Byte()
        Dim fs As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
        Dim br As BinaryReader = New BinaryReader(fs)
        Dim photo() As Byte = br.ReadBytes(fs.Length)
        br.Close()
        fs.Close()
        Return photo
    End Function

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        '  CheckBox1.CheckState = CheckState.Indeterminate
        ErrorProvider1.SetError(NombreTextBox, Nothing)
        ErrorProvider1.SetError(ApellidoTextBox, Nothing)
        ErrorProvider1.SetError(SexoComboBox, Nothing)
        ErrorProvider1.SetError(EstadocivilComboBox, Nothing)
        ErrorProvider1.SetError(Telefono1TextBox, Nothing)
        ErrorProvider1.SetError(IdcargoLookUpEdit, Nothing)
        ErrorProvider1.SetError(IdempleadoTextBox, Nothing)
        ErrorProvider1.SetError(EstaactivoCheckBox, Nothing)
        Me.EmpleadosTableAdapter.Fill(Me.DbFireAndIceDataSet.empleados)

        Mostrar()

    End Sub

   




    Private Sub frmEmpleado_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.horarios' table. You can move, or remove it, as needed.
        Me.HorariosTableAdapter.Fill(Me.DbFireAndIceDataSet.horarios)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.cargos' table. You can move, or remove it, as needed.
        Me.CargosTableAdapter.Fill(Me.DbFireAndIceDataSet.cargos)
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.empleados' table. You can move, or remove it, as needed.
        Me.EmpleadosTableAdapter.Fill(Me.DbFireAndIceDataSet.empleados)
        Fotografia1PictureBox.Image = My.Resources.url2
        Dim ms As New IO.MemoryStream
        Fotografia1PictureBox.Image.Save(ms, Imaging.ImageFormat.Jpeg)
        Dim bytes() As Byte = ms.ToArray
        photo = bytes


    End Sub

    Private Sub EmpleadosBindingNavigatorSaveItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmpleadosBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.EmpleadosBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim cam As New frmcamara2(Me)
        cam.ShowDialog()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim openFile As New System.Windows.Forms.OpenFileDialog()
        Dim img As Byte

        openFile.DefaultExt = "Picture"
        openFile.Filter = "jpg (*.jpg)|*.jpg|png (*.png*)|*.png*"

        openFile.ShowDialog()
        If openFile.FileNames.Length > 0 Then

            For Each filename In openFile.FileNames
                Fotografia1PictureBox.Image = System.Drawing.Image.FromFile(openFile.FileName)
                Fotografia1PictureBox.SizeMode = PictureBoxSizeMode.StretchImage

                photo = getphoto(openFile.FileName)


            Next
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.CheckState = CheckState.Checked Then

            IdempleadoTextBox.Properties.DisplayFormat.FormatString = ""
            IdempleadoTextBox.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.None
            IdempleadoTextBox.Properties.Mask.EditMask = ""
            IdempleadoTextBox.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.None
            IdempleadoTextBox.Text = ""
        Else
            IdempleadoTextBox.Properties.DisplayFormat.FormatString = "\d\d\d\d\d\d\d\d\d\d\d\d\d"
            IdempleadoTextBox.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            IdempleadoTextBox.Properties.EditFormat.FormatString = ""
            IdempleadoTextBox.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Custom
            IdempleadoTextBox.Properties.Mask.EditMask = "\d\d\d\d\d\d\d\d\d\d\d\d\d"
            IdempleadoTextBox.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx
            IdempleadoTextBox.Text = ""
        End If
    End Sub

    Private Sub Telefono1TextBox_KeyPress_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Telefono1TextBox.KeyPress
        If ((e.KeyChar <> Chr(8)) And (e.KeyChar <= Chr(47)) Or (e.KeyChar >= Chr(58))) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Advertencia", "Solo puede ingresar números")
            Telefono1TextBox.Focus()

        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Eroor", "No debe ingresar espacios")
            Telefono1TextBox.Focus()

        Else
            e.Handled = False

        End If
    End Sub

    Private Sub Telefono2TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Telefono2TextBox.KeyPress
        If ((e.KeyChar <> Chr(8)) And (e.KeyChar <= Chr(47)) Or (e.KeyChar >= Chr(58))) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Advertencia", "Solo puede ingresar números")
            Telefono2TextBox.Focus()

        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Eroor", "No debe ingresar espacios")
            Telefono2TextBox.Focus()

        Else
            e.Handled = False

        End If
    End Sub

    Private Sub CorreoelectronicoTextBox_Leave_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CorreoelectronicoTextBox.Leave
        Dim strCorreo As String
        strCorreo = CorreoelectronicoTextBox.Text
        Dim blnCorreo As Boolean
        Dim rgx As New System.Text.RegularExpressions.Regex("\w+([-+.´]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")
        If rgx.IsMatch(strCorreo) Then
            blnCorreo = True

        Else
            blnCorreo = False
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Error", "El Formato de correo electrónico no es válido")

            CorreoelectronicoTextBox.Focus()
        End If
    End Sub

    Private Sub EstaturaTextEdit1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles EstaturaTextEdit1.KeyPress
        If (((e.KeyChar <= Chr(47)) And (e.KeyChar <> Chr(8)) And (e.KeyChar <> Chr(46))) Or (e.KeyChar >= Chr(58))) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Advertencia", "Solo puede ingresar números")
            EstaturaTextEdit1.Focus()

        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Eroor", "No debe ingresar espacios")
            EstaturaTextEdit1.Focus()

        Else
            e.Handled = False

        End If
    End Sub

    Private Sub PesoTextEdit1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles PesoTextEdit1.KeyPress
        If (((e.KeyChar <= Chr(47)) And (e.KeyChar <> Chr(8)) And (e.KeyChar <> Chr(46))) Or (e.KeyChar >= Chr(58))) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Advertencia", "Solo puede ingresar números")
            PesoTextEdit1.Focus()

        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = True
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, "Eroor", "No debe ingresar espacios")
            PesoTextEdit1.Focus()

        Else
            e.Handled = False

        End If
    End Sub

    Private Sub GroupControl1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupControl1.Paint

    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub IdempleadoTextBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdempleadoTextBox.Leave
        TextBox22.Text = IdempleadoTextBox.Text



    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox22.TextChanged


    End Sub

    Private Sub EmpleadosBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmpleadosBindingSource.CurrentChanged

    End Sub
End Class